-module(queue_ui).
-include("rec_dec.hrl").
-export([q_table/0,listview/1,panel_view/1,panel_view/2,table_view/0]).
q_table() ->
	L = biz_queue:items(list),
	{table,[],
		lists:map(
			fun(Item)->
            QueueRec = Item#queue_inst_rec.q_rec,
            {
                tr,[],[
					{td,[],QueueRec#queue_rec.name},
					{td,[],QueueRec#queue_rec.desc},
					{td,[],QueueRec#queue_rec.max_len},
					{td,[],[{a,[{class,"button add_mob_bt"},{href,io_lib:format("get_dlg.yaws?mod=cust&action=add&queue=~s",[QueueRec#queue_rec.name])}],"Add"}]
					}
				]
            } end,L)
    }.

listview(accordion) ->
    L = biz_queue:items(list),
    {'div',[{id,"queue_list"}],
        lists:map(
            fun(X)->
                QueueRec = X#queue_inst_rec.q_rec,
                Name = QueueRec#queue_rec.name,
                [
                {
                    h3,[],[{a,[{href,"#"}],io_lib:format("~s:~s 最大队列长度~p 当前队列长度:~p",[
                                                    QueueRec#queue_rec.name,
                                                    QueueRec#queue_rec.desc,
                                                    QueueRec#queue_rec.max_len,
                                                    X#queue_inst_rec.in_q_num])
                      }]
                },
                {
                'div',[{class,"queue_area"}],[
                            {'span',[{class,"queue_cmd_area"}],[{
										input,[{id,"queue_name"},{type,"hidden"},{value,io_lib:format("~s",[Name])}],[]
									},{
                                    a,[{class,"button add_mob_bt"},{href,io_lib:format("get_dlg.yaws?mod=cust&action=add&queue=~s",[Name])}],"Add"
                                    },
                                    {a,[{class,"button refresh_q_bt"},{href,io_lib:format("queue_act.yaws?mod=queue&action=panel_view&name=~s",[Name])}],"Refresh"}
                                    ]
                            },
                            {'span',[{class,"queue_state"},{queue,io_lib:format("~s",[Name])}],[panel_view(q_name,Name)]}
                            
                        ]
                }
                ]end,
                L)
    }.
panel_view(Req) ->
    {value,{"name",Name}} = lists:keysearch("name",1,Req),
    {ehtml,panel_view(q_name,Name)}.

panel_view(q_name,QName) ->
	Items = queue_inst:get_cust_info(QName),
    lists:map(fun(#cust_in_q_rec{name=Name,mobile_phone=Mobile_Phone,in_q_time=InQ_Time,loc=Loc,sms_sent_times=SMSRec}) -> 
                {span,[{class,"cust_node"}],
				 	[{'div',[],io_lib:format("~ts:~ts 排队时间:~p 序号:~p:短消息发送次数~p:",[Name,Mobile_Phone,InQ_Time,Loc,SMSRec])},
					 {'div',[],[
							{a,[{class,"cust_node_bt"},{href,io_lib:format("queue_act.yaws?mod=cust&action=pick&queue=~s&name=~s",[QName,Name])}],"P"},
							{a,[{href,io_lib:format("queue_act.yaws?mod=cust&action=pick_header&name=~s",[Name])}],"S"},
							{a,[{href,io_lib:format("queue_act.yaws?mod=cust&action=pick_header&name=~s",[Name])}],"D"} 
							 ]}
					]
                }
            end,Items).


table_view() ->
    {table,[{id,"queue_view"}],gen_row(2,1)}.

gen_row(Row,Col) when Row /= 0 -> 
    [{tr,[],gen_col(Col)}|gen_row(Row - 1,Col)];

gen_row(0,Col) ->
    {tr,[],gen_col(Col)}.

gen_col(Col) when Col /= 0 -> 
    [{td,[],[queue_node()]} | gen_col(Col-1)];
gen_col(0) ->
    {td,[],[queue_node()]}.
    
queue_node() ->
    {'div',[{class,"queue_node"}]," + "}.
    
    